import { cloudService } from './index'
import { IBlog, IBlogAdjacent, IBlogCategory, IBlogComment, IBlogSearch, IBlogStats, IQueryBlog } from '@/types/blog'

import qs from 'qs'
import { IPageRecord } from '@/types'

export const queryBlogApi = (currentPage: number, pageSize: number): Promise<IPageRecord<IBlog>> =>
  cloudService({
    url: '/blog-api/blog/page',
    method: 'get',
    params: { currentPage, pageSize }
  })

export const searchBlogApi = (keyword: string): Promise<IBlogSearch[]> =>
  cloudService({
    url: '/blog-api/blog/search',
    method: 'get',
    params: { keyword }
  })

export const queryBlogByConditionApi = (query: IQueryBlog): Promise<IBlog[]> =>
  cloudService({
    url: '/blog-api/blog/condition',
    method: 'get',
    params: query,
    paramsSerializer: (params) => {
      return qs.stringify(params, { arrayFormat: 'indices' })
    }
  })

export const queryBlogByIdApi = (id: number): Promise<IBlog> =>
  cloudService({
    url: `/blog-api/blog/${id}/content`,
    method: 'get'
  })

export const queryBlogCategoryApi = (): Promise<IBlogCategory[]> =>
  cloudService({
    url: '/blog-api/blog/category',
    method: 'get'
  })

export const queryBlogStatsApi = (): Promise<IBlogStats> =>
  cloudService({
    url: '/blog-api/blog/stats',
    method: 'get'
  })

export const queryLatestBlogApi = (): Promise<IBlog[]> =>
  cloudService({
    url: '/blog-api/blog/latest',
    method: 'get'
  })

export const queryBlogAdjacentApi = (blogId: number): Promise<IBlogAdjacent[]> =>
  cloudService({
    url: `/blog-api/blog/${blogId}/adjacent`,
    method: 'get'
  })

export const queryBlogCommentApi = (blogId: number): Promise<IBlogComment[]> =>
  cloudService({
    url: `/blog-api/blog/${blogId}/comment`,
    method: 'get'
  })

export const addBlogCommentApi = (blogId: number, blogComment: IBlogComment): Promise<boolean> =>
  cloudService({
    url: `/blog-api/blog/${blogId}/comment`,
    method: 'put',
    data: blogComment
  })
